package com.greenline.search.test.utils;

import com.greenline.search.report.entity.SearchWord;
import com.greenline.search.test.dao.AnalyzeObject;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


public class JSONStreamParserForAnalyze {
    private static JsonFactory factory = new JsonFactory();
    private static JsonParser parser;
    public static List<AnalyzeObject> parse(String url) throws MalformedURLException, IOException {
        // get an instance of the json parser from the json factory
        parser = factory.createJsonParser(new URL(url));
        List<AnalyzeObject> objectList = new ArrayList<AnalyzeObject>();
        // continue parsing the token till the end of input is reached
        while (!parser.isClosed()) {
            // get the token
            JsonToken token = parser.nextToken();
            // if its the last token then we are done
            if (token == null){
                break;
            }
            if (JsonToken.FIELD_NAME.equals(token) && "uuid".equals(parser.getCurrentName())) {
                parser.nextToken();
                AnalyzeObject object = new AnalyzeObject();
                object.setUuid(parser.getText());
                parser.nextToken();
                parser.nextToken();
                object.setType(parser.getText());
                parser.nextToken();
                parser.nextToken();
                object.setKey(parser.getText());
                objectList.add(object);
            }
        }

        return objectList;
    }

    public static List<String> parseDoctorSearchResponse(String url) throws MalformedURLException, IOException {
        // get an instance of the json parser from the json factory
        parser = factory.createJsonParser(new URL(url));
        List<String> objectList = new ArrayList<String>();
        // continue parsing the token till the end of input is reached
        while (!parser.isClosed()) {
            // get the token
            JsonToken token = parser.nextToken();
            // if its the last token then we are done
            if (token == null){
                break;
            }
            if (JsonToken.FIELD_NAME.equals(token) && "department_uuid".equals(parser.getCurrentName())) {
                parser.nextToken();
                parser.nextToken();
                objectList.add(parser.getText());
            }
        }

        return objectList;
    }

    /**
     * 根据一个关键词获取相应的填充好tokenArray和typeArray的SearchWord
     * @param word
     * @return
     */
    public static SearchWord getWordType(String word){
        SearchWord searchWord = new SearchWord();
        String url = "http://192.168.1.37:8098/analyze/?q=";
        List<AnalyzeObject> analyzeObjectList = null;
        try {
            url += URLEncoder.encode(word,"UTF-8");
            analyzeObjectList = parse(url);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String[] tokenArray = new String[analyzeObjectList.size()];
        String[] typeArray = new String[analyzeObjectList.size()];
        for(int i = 0; i < analyzeObjectList.size(); i++){
            tokenArray[i] = analyzeObjectList.get(i).getKey();
            typeArray[i] = analyzeObjectList.get(i).getType();
        }
        searchWord.setTokenArray(tokenArray);
        searchWord.setTypeArray(typeArray);
        return searchWord;
    }

    public static void main(String[] args) throws IOException {
        SearchWord word = getWordType("傅雯雯华山医院");

        for(int i = 0; i < word.getTokenArray().length; i++){
            System.out.println(word.getTokenArray()[i]);
            System.out.println(word.getTypeArray()[i]);
            System.out.println();
        }
    }

}